package com.lmqb.controller;

import com.lmqb.bean.JCollection;
import com.lmqb.bean.SysUser;
import com.lmqb.page.CollectionPager;
import com.lmqb.service.AccountService;
import com.lmqb.service.JiaoYanService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.UUID;

@Controller
public class ExcelController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private AccountService accountService;
	
	
	@RequestMapping(value="collectionExcel.html",method=RequestMethod.GET)
	public String dakuan(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,CollectionPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(CollectionPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			CollectionPager userBankPager=accountService.selectCollectionPager(pagerModel);
			System.out.println("&&&&&&&&&&&&&&&"+userBankPager.getList());
		   // 第一步，创建一个webbook，对应一个Excel文件  
	        HSSFWorkbook wb = new HSSFWorkbook();  
	        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
	        HSSFSheet sheet = wb.createSheet("学生表一");  
	        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
	        HSSFRow row = sheet.createRow((int) 0);  
	        // 第四步，创建单元格，并设置值表头 设置表头居中  
	        HSSFCellStyle style = wb.createCellStyle();  
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
	        HSSFCell cell = row.createCell((short) 0);  
	        cell.setCellValue("还款订单号");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 1);  
	        cell.setCellValue("用户ID");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 2);  
	        cell.setCellValue("用户名");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 3);  
	        cell.setCellValue("手机号");  
	        cell.setCellStyle(style);
	        cell = row.createCell((short) 4);  
	        cell.setCellValue("借款金额");  
	        cell.setCellStyle(style); 
	        cell = row.createCell((short) 5);  
	        cell.setCellValue("借款时间");  
	        cell.setCellStyle(style);
	        cell = row.createCell((short) 6);  
	        cell.setCellValue("预计还款时间");  
	        cell.setCellStyle(style);
	        cell = row.createCell((short) 7);  
	        cell.setCellValue("逾期天数");  
	        cell.setCellStyle(style);
	        cell = row.createCell((short) 8);  
	        cell.setCellValue("还款状态");  
	        cell.setCellStyle(style); 
	        cell = row.createCell((short) 9);  
	        cell.setCellValue("催收备注");  
	        cell.setCellStyle(style);
	        cell = row.createCell((short) 10);  
	        cell.setCellValue("催收人");  
	        cell.setCellStyle(style);
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        for(int i=0;i<userBankPager.getList().size();i++){
	        	 row = sheet.createRow((int) i + 1);
	        	 JCollection coll = (JCollection) userBankPager.getList().get(i);
	        	 row.createCell((short) 0).setCellValue( coll.getRepayNo());
	        	 row.createCell((short) 1).setCellValue(coll.getUserId());
	        	 row.createCell((short) 2).setCellValue(coll.getUserName());
	        	 row.createCell((short) 3).setCellValue(coll.getUserMobile());
	        	 row.createCell((short) 4).setCellValue((coll.getLoanAmount()).toString());
	        	 row.createCell((short) 5).setCellValue(formatter.format(coll.getLoanTimeD()));
	        	 row.createCell((short) 6).setCellValue(formatter.format(coll.getPlanRepayTimeD()));
	        	 row.createCell((short) 7).setCellValue(coll.getOverdueDays());
	        	 if(coll.getRepayStatus()==0){
	        	 row.createCell((short) 8).setCellValue("待还款");
	        	 }if(coll.getRepayStatus()==1){
		        	 row.createCell((short) 8).setCellValue("还款中");
		        	 } if(coll.getRepayStatus()==2){
			        	 row.createCell((short) 8).setCellValue("还款成功");
		        	 } if(coll.getRepayStatus()==3){
			        	 row.createCell((short) 8).setCellValue("提前还款");
		        	 }
	        	 row.createCell((short) 9).setCellValue(coll.getRemark());
	        	 row.createCell((short) 10).setCellValue(coll.getAdminName());
	        	
	        	
	        }
        	
	        String uuid = UUID.randomUUID().toString().replace("-", "");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename="+uuid+".xls");
            wb.write(response.getOutputStream());
            response.getOutputStream().flush();
            response.flushBuffer();
            response.getOutputStream().close();

          
		}catch(Exception e){
			e.printStackTrace();
		}
			
		
	 return "redirect:collection.html";
	
}
	
	
}